package vote.ai.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * Description:文本纠错工具类
 * Author:han
 * Date: 2024-05-21
 */
public class CorrectionUtil {
    // 获取 ----- 后面的字符串
    public static List<Long> getAfterDash(String input) {
        int dashIndex = input.indexOf("-----");
        if (dashIndex != -1 && dashIndex + 5 < input.length()) {
            String s = input.substring(dashIndex + 5).trim();
            // 将 s 按 空格分割，并返回一个 Long 类型的列表
            if (StringUtils.hasText(s)) {
                return Arrays.stream(s.split(",")).map(Long::parseLong).collect(Collectors.toList());
            }
        }
        return new ArrayList<>();
    }
    /**
     * 移除文本中的"-----"及其后面的内容
     *
     * @param input 输入文本
     * @return 处理后的文本
     */
    public static String removeAfterDash(String input) {
        int dashIndex = input.indexOf("-----");
        if (dashIndex != -1) {
            return input.substring(0, dashIndex).trim();
        }
        return input.trim();
    }

    /**
     * 按照换行符分割字符
     *
     * @param text 原文本
     * @return 分割结果
     */
    public static List<TextSegment> splitTextV1(String text) {
        //获取分割字符
        List<String> separatorList = getSeparatorList();
        //转为正则
        String regularExpression = separatorList.stream().map(Pattern::quote).collect(Collectors.joining("|"));
        Pattern pattern = Pattern.compile(regularExpression);
        Matcher matcher = pattern.matcher(text);
        int order = 1;
        int lastEnd = 0;
        List<TextSegment> result = new ArrayList<>();
        while (matcher.find()) {
            //截取匹配到的文本分段
            String str = text.substring(lastEnd, matcher.end());
            if (lastEnd != matcher.start() || str.length() == 1) {
                //如果上次分割文本位置和当前分割的开始位置不同证明中间有文本未处理
                if (matcher.end() != text.length() && text.charAt(matcher.end()) == '"') {
                    //当前匹配到了句子结束需要加上结束符号
                    String segmentText = text.substring(lastEnd, matcher.end() + 1);
                    if (matcher.group().equals("\n")) {
                        segmentText = segmentText + "\n";
                    }
                    result.add(new TextSegment(order++, segmentText, lastEnd, matcher.end()));
                    lastEnd = matcher.end() + 1;
                } else {
                    String segmentText = text.substring(lastEnd, matcher.end());
                    if (matcher.group().equals("\n")) {
                        segmentText = segmentText + "\n";
                    }
                    result.add(new TextSegment(order++, segmentText, lastEnd, matcher.end()));
                    lastEnd = matcher.end();
                }
            }
        }
        if (lastEnd < text.length()) {
            result.add(new TextSegment(order, text.substring(lastEnd), lastEnd, text.length() - 1));
        }
        return result.stream().filter(segment -> !segment.getText().isEmpty()).collect(Collectors.toList());
    }

    private static List<String> getSeparatorList() {
        return Arrays.asList("/n", "//n");
    }

    @Data
    @AllArgsConstructor
    public static class TextSegment {
        private int index;
        private String text;
        private int startIndex;
        private int endIndex;
    }
}
